<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 2016/3/24
 * Time: 14:29
 */

namespace Admin\Controller;

class UserController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
    }

    /**
     * 用户管理
     */
    public function Manage(){
        $name = I('get.name');
        $phone = I('get.phone');
        $start_time = I('get.start_time');
        $end_time = I('get.end_time');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);

        if($name){
            $map['usr.user_name'] = array('like',"%$name%");
        }
        if($phone){
            $map['usr.phone'] = array('like',"%$phone%");
        }
        if($start_time && $end_time) {
            $map['usr.add_time'] = array('between',$start_time.','.$end_time);
        }elseif($start_time){
            $map['usr.add_time'] = array('egt',$start_time);
        }
        $order_count = M('repair_order')->where('status != -1')->count();
        $order_total = M('repair_order')->field('SUM(total) as total')->where('status != -1')->find();

        $count = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.user_status,usr.user_face,usr.user_name,usr.phone,usr.invite as invite_code,usr.add_time,usr.user_status,addr.address,addr.status')
            ->join('LEFT JOIN zsf_address addr on usr.user_id = addr.user_id and addr.status = 1 and addr.is_del !=1')
            ->where($map)->count();
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show  = $Page->show();// 分页显示输出

        $data = M()->table('zsf_user usr')
                ->field('usr.user_id,usr.user_status,usr.user_face,usr.user_name,usr.phone,usr.invite as invite_code,usr.add_time,usr.user_status,addr.address,addr.status')
                ->join('LEFT JOIN zsf_address addr on usr.user_id = addr.user_id and addr.status = 1 and addr.is_del !=1')
                ->where($map)
                ->order(array('usr.user_id'=>'asc'))
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        foreach($data as $k=>$v){
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$data[$k]['add_time']);
            $data[$k]['user_face'] = $data[$k]['user_face'].'?imageView2/1/w/75/h/65';
        }
        $this->breadcrumb1='用户管理';
        $this->breadcrumb2='用户';
        $this->assign('count',$order_count);
        $this->assign('total',$order_total['total']);
        $this->assign('user',$data);
        $this->assign('page',$show);
        $this->display();
    }

    /**
     * 基本信息
     */

    public function info(){
        $user_id = I('get.user_id');
        $type    = I('get.type','info');
        if($type == 'master'){
            $this->breadcrumb1='师傅管理';
            $this->breadcrumb2='师傅';
            $this->breadcrumb3='基本资料';
            $this->url = U('Master/index');
        }else{
            $this->breadcrumb1='用户管理';
            $this->breadcrumb2='用户';
            $this->breadcrumb3='基本信息';
            $this->url=U('User/Manage');
        }
        $data = M('user')
            ->field('zsf_user.user_id,zsf_user.user_face,zsf_user.user_name,zsf_user.phone,zsf_user.invite as invite_code,zsf_user.invite,zsf_user.is_real,zsf_user_real_info.id_name,zsf_user_real_info.id_num')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = zsf_user.user_id','left')
            ->where('zsf_user.user_id = '.$user_id)
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['user_face'] = $data[$k]['user_face'].'?imageView2/1/w/75/h/65';
        }
        $this->assign('info',$data);
        $this->assign('type',$type);
        $this->display();
    }

    /**
     * 登录记录
     */

    public function login(){
        $user_id = I('get.user_id');
        $type    = I('get.type','info');
        if($type == 'master'){
            $this->breadcrumb1='师傅管理';
            $this->breadcrumb2='登录记录';
            $this->url = U('Master/index');
        }else{
            $this->breadcrumb1='用户管理';
            $this->breadcrumb2='用户登录记录';
        }
        $info = M('user_login_log')->where('user_id = '.$user_id)->order('login_time desc')->select();
        $count = M('user_login_log')->where('user_id = '.$user_id)->count();
        foreach($info as $k=>$v){
            $info[$k]['login_time'] = date('Y-m-d H:i:s',$info[$k]['login_time']);
        }
        $this->assign('count',$count);
        $this->assign('login',$info);
        $this->assign('type',$type);
        $this->display();
    }

    /**
     * 地址管理
     */
    public function address(){
        $this->breadcrumb1='用户管理';
        $this->breadcrumb2='用户地址';
        $user_id = I('get.user_id');
        $data = M('address')->field('contacts,address,phone,status')->where("user_id = $user_id and is_del !=1")->select();
        $this->assign('address',$data);
        $this->display();
    }

    /**
     * 加黑名单
     */
    public function blacklist(){
        $user = I('post.user_id');
        $master = I('post.worker_user_id');
        $restore = I('post.restore');

        if($restore && $user){
            $status = M('user')->field('user_status')->where("user_id = $user")->find();
            if($status['user_status']=='3'){
                $data['user_status'] = '1';
            }else{
                $data['user_status'] = '0';
            }
            $data['user_id'] = $user;
        }elseif($restore && $master){
            $status = M('user')->field('master_status')->where("user_id = $master")->find();
            if($status['master_status']=='3'){
                $data['master_status'] = '1';
            }else{
                $data['master_status'] = '0';
            }
            $data['user_id'] = $master;
        }elseif($user && !$restore){
            $status = M('user')->field('user_status')->where("user_id = $user")->find();
            if($status['user_status']=='1'){
                $data['user_status'] = '3';
            }else{
                $data['user_status'] = '2';
            }
            $data['user_id'] = $user;
        }elseif($master && !$restore){
            $status = M('user')->field('master_status')->where("user_id = $master")->find();
            if($status['master_status']=='1'){
                $data['master_status'] = '3';
            }else{
                $data['master_status'] = '2';
            }
            $data['user_id'] = $master;
        }

        $info = M('user')->save($data);
        if($info){
            echo 1;
        }else{
            echo 2;
        }

    }

    /**
     * 锁定用户
     */

    public function lock(){
        $user = I('post.user_id');
        $master = I('post.worker_user_id');
        $restore = I('post.restore');

        if($restore && $user){
            $status = M('user')->field('user_status')->where("user_id = $user")->find();
            if($status['user_status']=='3'){
                $data['user_status'] = '2';
            }else{
                $data['user_status'] = '0';
            }
            $data['user_id'] = $user;

        }elseif($restore && $master){
            $status = M('user')->field('master_status')->where("user_id = $master")->find();
            if($status['master_status']=='3'){
                $data['master_status'] = '2';
            }else{
                $data['master_status'] = '0';
            }
            $data['user_id'] = $master;

        }elseif($user && !$restore){
            $status = M('user')->field('user_status')->where("user_id = $user")->find();
            if($status['user_status']=='2'){
                $data['user_status'] = '3';
            }else{
                $data['user_status'] = '1';
            }
            $data['user_id'] = $user;

        }elseif($master && !$restore){
            $status = M('user')->field('master_status')->where("user_id = $master")->find();
            if($status['master_status']=='2'){
                $data['master_status'] = '3';
            }else{
                $data['master_status'] = '1';
            }
            $data['user_id'] = $master;
        }

        $info = M('user')->save($data);
        if($info){
            echo 1;
        }else{
            echo 2;
        }
    }

}